package cn.xx.pet.cotroller;

import cn.xx.basic.util.AjaxResult;
import cn.xx.basic.util.PageList;
import cn.xx.org.domain.Department;
import cn.xx.org.domain.Employee;
import cn.xx.pet.domain.MasterMsg;
import cn.xx.pet.domain.Pet;
import cn.xx.pet.query.MasterMsgQuery;
import cn.xx.pet.query.PetQuery;
import cn.xx.pet.service.IMasterMsgService;
import cn.xx.user.domain.LoginInfo;
import cn.xx.user.domain.User;
import cn.xx.user.mapper.UserMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/searchMasterMsg")
public class MasterMsgController {
    @Autowired
    private IMasterMsgService masterMsgService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired UserMapper userMapper;

    // 宠物领养发布
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody MasterMsg masterMsg, HttpServletRequest request){
        try {
            //获取token
            String token = request.getHeader("TOKEN");
            LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
            //根据id 查询用户
            User user = userMapper.findByLoginInfo(loginInfo.getId());
            masterMsg.setUser_id(user.getId());
            //添加 发布用户
            masterMsgService.publish(masterMsg);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
        return AjaxResult.me();
    }

    //分页查询接口
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "")
    public PageList<MasterMsg> pageQuery(@RequestBody MasterMsgQuery masterMsgQuery, HttpServletRequest request) {
        //获取token
        String token = request.getHeader("TOKEN");
        LoginInfo loginInfo = (LoginInfo) redisTemplate.opsForValue().get(token);
        // 通过登录表id查询员工
        Employee employee = masterMsgService.findByLogin(loginInfo.getId());
        // 将员工的店铺id设置进去 只显示本员工的店铺消息
        masterMsgQuery.setShop_id(employee.getShop_id());
        return masterMsgService.pageQuery(masterMsgQuery);
    }

    // 删除
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            masterMsgService.delete(id);
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }

    // 修改/添加
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody MasterMsg masterMsg) {
        try {
            if (masterMsg.getId() == null) {
                masterMsgService.save(masterMsg);
            } else {
                masterMsgService.update(masterMsg);
            }
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }
}
